package evil.wolf.business;

import evil.wolf.db.DatabaseManager;
import evil.wolf.excute.query.ExcuteQuery;
import evil.wolf.objects.ObjectMaterial;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JOptionPane;


public class BusinessMaterial {
	
	DatabaseManager databaseManager;	
//	ObjectMaterial objectMaterial;
	ExcuteQuery exc;
	public int colCount;
	public String colName[];

	public BusinessMaterial() {
		
//		this.objectMaterial = objectMaterial;
		databaseManager = new DatabaseManager();
		exc = new ExcuteQuery();
		
	}
	
	public int getColumn(){
		return colCount;
	}
	
	public String[] getName(){
		return colName;
	}
	public List<ObjectMaterial> getListRecords() {
		
		List<ObjectMaterial> rowList = new ArrayList<>();
		
		try {
			ResultSet rs = databaseManager.getResultSet(exc.selectData());
			ResultSetMetaData meta = rs.getMetaData();
			colCount = meta.getColumnCount();
			colName = new String[colCount];
			
			for (int h = 0; h < colCount; h++) {
				colName[h] = meta.getColumnName(h + 1);
			}
			
			while (rs.next()){
				
				ObjectMaterial newObjectMaterial = new ObjectMaterial();
				
				newObjectMaterial.setmah(rs.getString(1));
				newObjectMaterial.settenh(rs.getString(2));
				newObjectMaterial.setmanh(rs.getString(3));
				newObjectMaterial.setdvt(rs.getString(4));
				newObjectMaterial.setsl(rs.getInt(5));
				newObjectMaterial.setdg(rs.getString(6));
				
				
				rowList.add(newObjectMaterial);
			}
			databaseManager.closeConnect();
		} catch (SQLException e) {

			JOptionPane.showMessageDialog(null,
					e.toString(), "ERROR", JOptionPane.INFORMATION_MESSAGE);
			e.printStackTrace();
		}
		return rowList;
	}
}
